#!/usr/bin/env bash

set -e

cd "$(dirname "$0")/.."

if ! command -v uv >/dev/null 2>&1; then
    echo "uv is required (https://github.com/astral-sh/uv)" >&2
    exit 1
fi

case "$1" in
    stable)
        # install the latest stable version of home assistant
        uv pip install --force-reinstall homeassistant
        ;;
    beta)
        # install the latest beta version of home assistant
        uv pip install --upgrade --pre homeassistant
        ;;
    dev)
        # install the latest dev version of home assistant
        uv pip install --upgrade git+https://github.com/home-assistant/core.git@dev
        ;;
    version)
        # print the version of home assistant
        python3 -m homeassistant --version
        ;;
    help)
        # print the help message
        echo "Usage: $0 [stable|beta|dev|version|<version>]"
        ;;
    list)
        # list all available versions of home assistant
        uv pip index versions homeassistant --pre
        ;;                
    *)         
        # if first argument matches a version number, install that version ie 0.115.6 or 0.116.0b0
        if [[ $1 =~ ^[0-9]+\.[0-9]+(\.[0-9]+)?(b[0-9]+)?$ ]]; then
            uv pip install --upgrade homeassistant==$1
        else
            # otherwise print the help message
            echo "Usage: $0 [stable|beta|dev|version|list|<version>]"
        fi
        ;;
esac

